﻿using System;
using System.Windows.Forms;
using System.Data;

namespace FinanceManager
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Hide();
            LoginForm form = new LoginForm();
            form.ShowDialog();
            if(form.DialogResult==DialogResult.OK){
                this.Show();
            }
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ShowWorkList();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePsw cpw = new ChangePsw();
            cpw.ShowDialog();
        }

        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowWorkers();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ShowWorkers()
        {
            dgv.DataSource = workerTableAdapter.GetData();
            删除此过期工期ToolStripMenuItem.Enabled = false;
            工期结束ToolStripMenuItem.Enabled = false;
            删除工人ToolStripMenuItem.Enabled = true;
        }

        public void ShowWorkList()
        {
            dgv.DataSource = adapterWorkWithWorker1.GetData();
            删除此过期工期ToolStripMenuItem.Enabled = false;
            删除工人ToolStripMenuItem.Enabled = false;
            工期结束ToolStripMenuItem.Enabled = true;
        }

        public void ShowOutDateWorkList()
        {
            dgv.DataSource = outDateWorkListTableAdapter1.GetOutDateWorkList();
            删除此过期工期ToolStripMenuItem.Enabled = true;
            删除工人ToolStripMenuItem.Enabled = false;
            工期结束ToolStripMenuItem.Enabled = false;
        }

        private void 添加工人ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddWorker form = new AddWorker();
            form.ShowDialog();

            if (form.DialogResult == DialogResult.OK)
            {
                ShowWorkers();
            }
        }

        private void 查看工期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowWorkList();
        }

        private void 添加工期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewWork form = new AddNewWork();
            form.ShowDialog();

            if (form.DialogResult == DialogResult.OK)
            {
                ShowWorkList();
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("该功能未实现");
        }

        private void 删除工人ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (workerTableAdapter.DeleteWorkerById(Convert.ToInt32(dgv.CurrentRow.Cells[0].Value)) == 1)
            {
                MessageBox.Show("删除成功！");
                ShowWorkers();
            }
        }

        private void 工期结束ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            long totolfee=(DateTime.Parse(dgv.CurrentRow.Cells[1].Value.ToString()) - DateTime.Now).Days * int.Parse(dgv.CurrentRow.Cells[4].Value.ToString());

            workListTableAdapter.WorkEndize(DateTime.Now,totolfee*-1, long.Parse(dgv.CurrentRow.Cells[0].Value.ToString()));
            MessageBox.Show("应付总工资为：" + totolfee*-1 +"元", "结帐");
            ShowWorkList();
        }

        private void 查看历史工期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOutDateWorkList();
        }

        private void 删除此过期工期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (workListTableAdapter.DeleteOutdateWorkListById(long.Parse(dgv.CurrentRow.Cells[0].Value.ToString())) != 0) {
                MessageBox.Show("删除成功！");
            }
            ShowOutDateWorkList();
        }

        private void 清空历史工期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (workListTableAdapter.DeleteOutDateWorkList() != 0) {
                MessageBox.Show("清空成功！");
            }
            ShowOutDateWorkList();
        }
    }
}
